package com.xyh.flowershop.controller;

import com.xyh.flowershop.annotations.LoggingRecord;
import com.xyh.flowershop.annotations.RefreshToken;
import com.xyh.flowershop.constant.RequestBaseConstant;
import com.xyh.flowershop.context.WebContext;
import com.xyh.flowershop.service.FileService;
import com.xyh.flowershop.service.UserService;
import com.xyh.flowershop.vo.req.user.UserReqVo;
import com.xyh.flowershop.vo.req.user.WxUserReqVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import com.xyh.flowershop.vo.resp.user.UserRespVo;
import com.xyh.flowershop.vo.resp.user.WxUserRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @author 小约翰可汗
 * @className UserController
 * @createTime 2024/4/2221:02
 */
@Slf4j
@RefreshToken
@LoggingRecord(excludeMethods = {"uploadFile"})
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user/")
@Tag(name = "用户接口信息")
public class UserController {

    private final UserService userService;

    private final FileService fileService;

    public UserController(UserService userService, FileService fileService) {
        this.userService = userService;
        this.fileService = fileService;
    }

    @Operation(description = "获得用户信息")
    @GetMapping("find/info")
    public ResultRespVo findUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        WxUserRespVo user = userService.findUser("", token);
        return ResultRespVo.success(user);
    }

    @Operation(description = "微信用户进行信息更改")
    @PostMapping("app/update")
    public ResultRespVo updateWxUser(@RequestBody WxUserReqVo vo){
//        log.info("当前线程{}",Thread.currentThread().getId());
        userService.updateSelf(vo);
        // 将更改后的信息进行重新回写
        return ResultRespVo.success("");
    }

    @Operation(description = "用户信息分页查询")
    @PostMapping("find/page")
    public  ResultRespVo findPageUsers(@RequestBody UserReqVo vo){
        return  ResultRespVo.success(userService.findPageUsers(vo));
    }

    @Operation(description = "根据编号查询用户信息")
    @GetMapping("find/{id}")
    public ResultRespVo findUser(@PathVariable String id){
        UserRespVo vo = userService.findUserById(id);
        return !Objects.isNull(vo) ? ResultRespVo.success(vo) : ResultRespVo.error("未查找到对应用户信息！");
    }

    @Operation(description = "头像上传")
    @PostMapping("avatar/upload")
    public ResultRespVo uploadFile(@RequestBody MultipartFile file, HttpServletRequest request){
        String path = RequestBaseConstant.BASE_URL+"/img/avatar/"+fileService.uploadFile(file, request.getRequestURI());
        return ResultRespVo.success(path);
    }

    @Operation(description = "删除用户信息")
    @DeleteMapping("delete/{id}")
    public ResultRespVo deleteUserInfo(@PathVariable String id){
        return userService.deleteUserInfo(id,WebContext.get());
    }
}
